package com.lmds.sprindemo1.mq;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

/**
 * Created with IntelliJ IDEA.
 * User: mgm
 * Date: 2019-08-22
 * Time: 11:18
 */

@Service
public class MessageProducter {

    @Autowired
    private JmsMessagingTemplate jms;
    /**
     *  发送消息到队列
     * @param aim
     * @param msg
     */
    public void sendMsg(String aim, String msg){
        Destination destination = new ActiveMQQueue(aim);
        jms.convertAndSend(destination, msg);
        System.err.println("send to "+aim + ":" + msg);
    }

    /**
     * 发布主题到队列
     *
     * @param aim
     * @param msg
     */
    public void publish(String aim, String msg){
        Destination destination = new ActiveMQTopic(aim);
        jms.convertAndSend(destination, msg);
        System.err.println("publish a topic "+aim + ":" + msg);
    }
}
